class Restaurants::OrdersController < Restaurants::RestaurantsController
  include PrintOrder
  before_action :get_order,only:[:show,:pay,:print_order,:finish_order,:destroy,:cancel]

  def index
      @nav_title = "订单管理"
      add_breadcrumb create_nav(@nav_title,'#')
      @orders = Order.where(status: params[:status].to_i,restaurant_id: session[:restaurant_id])
  end  

  def show
    @nav_title = "订单详情-#{@order.order_number}"
    add_breadcrumb create_nav('订单管理',"/restaurants/orders?stauts=#{@order.status}")
    add_breadcrumb create_nav(@nav_title,'#')
  end

  def destroy
    @order.destroy
    redirect_to action: :index
  end  

  def cancel
    @order.status = Order::CANCEL
    @order.save
    redirect_to action: :show,id: @order.id
  end  

  def pay
    @order.is_pay = true
    @order.status = Order::SUCCESS
    if @order.save
      content = generate_content @order
      printers_print @order.restaurant,content
    end  
    redirect_to action: :show,id: @order.id
  end  
  
  def print_order      
      content = generate_content @order
      printers_print @order.restaurant,content
      redirect_to action: :show,id: @order.id
  end  

  def finish_order
    @order.status = Order::FINISH
    @order.save
    redirect_to action: :show,id: @order.id
  end  

  private
  def get_order
    @order = Order.find params[:id]
  end  


end
